-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
64 lines (57 loc) · 1.77 KB
/
Solution.java
File metadata and controls
64 lines (57 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Scanner;
import java.util.Stack;
class MinStack {
private Stack<Integer> mainStack = new Stack<>();
private Stack<Integer> minStack = new Stack<>();
public void push(int value) {
mainStack.push(value);
if (minStack.isEmpty() || value <= minStack.peek()) {
minStack.push(value);
}
System.out.println("Pushed: " + value);
}
public void pop() {
if (mainStack.isEmpty()) {
System.out.println("Stack underflow.");
return;
}
if (mainStack.peek().equals(minStack.peek())) {
minStack.pop();
}
System.out.println("Popped: " + mainStack.pop());
}
public int getMin() {
if (minStack.isEmpty()) {
System.out.println("Stack is empty.");
return Integer.MAX_VALUE;
}
return minStack.peek();
}
}
public class MinStackDemo {
public static void main(String[] args) {
MinStack ms = new MinStack();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("\n1. Push\n2. Pop\n3. Get Minimum\n4. Exit\nEnter your choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter value to push: ");
int value = sc.nextInt();
ms.push(value);
break;
case 2:
ms.pop();
break;
case 3:
System.out.println("Minimum: " + ms.getMin());
break;
case 4:
return;
default:
System.out.println("Invalid choice.");
}
}
}
}